package com.czl.lunt.common;

import com.fasterxml.jackson.annotation.JsonInclude;

public class Result<T> {
    //无论任何情况这个字段都有参与序列化
    @JsonInclude(JsonInclude.Include.ALWAYS)
    private int code;
    // 描述信息
    @JsonInclude(JsonInclude.Include.ALWAYS)
    private String message;
    // 具体的数据
    @JsonInclude(JsonInclude.Include.ALWAYS)
    private T data;

    public Result(int code, String message) {
        this(code, message, null);
    }

    public Result(int code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }
    /**
     * 成功
     * @return
     */
    public static Result success () {
        return new Result(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMessage());
    }

    public static Result success (String message) {
        return new Result(ResultCode.SUCCESS.getCode(), message);
    }

    public static <T> Result<T> success (T data) {
        return new Result<>(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMessage(), data);
    }

    public static <T> Result<T> success (String message, T data) {
        return new Result<>(ResultCode.SUCCESS.getCode(), message, data);
    }

    /**
     * 失败
     * @return
     */
    public static Result failed () {
        return new Result(ResultCode.FAILED.getCode(), ResultCode.FAILED.getMessage());
    }

    public static Result failed (String message) {
        return new Result(ResultCode.FAILED.getCode(), message);
    }

    public static Result failed (ResultCode resultCode) {
        return new Result(resultCode.getCode(), resultCode.getMessage());
    }


    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }
}
